/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sisgraf.dao;
import sisgraf.model.Orcamento;
import sisgraf.util.HibernateUtil;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author Jordan de Carvalho Guerra
 */
public class OrcamentoDaoImp  implements OrcamentoDao {
    private Session sessionLock;
    private Transaction transacaoLock;
    public boolean save(Orcamento orcamento) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.save(orcamento);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
    public boolean update(Orcamento orcamento) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.update(orcamento);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
    public Orcamento getOrcamento(int id) {
        Orcamento orcamento=null;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            orcamento= (Orcamento) session.get(Orcamento.class, id);
        } catch(Exception e)
        {
            orcamento=null;
            if (session != null){
                session.close();
            }
        } finally
        {
            return orcamento;
        }
    }
    public Orcamento getLockOrcamento(int id) {
        Orcamento orcamento=null;
        try
        {
            this.sessionLock = HibernateUtil.getSessionFactory().openSession();
            this.transacaoLock = this.sessionLock.beginTransaction();
            orcamento = (Orcamento) this.sessionLock.get(Orcamento.class, id, LockMode.UPGRADE);
        } catch(Exception e) {
            orcamento=null;
            if (this.transacaoLock != null){
                this.transacaoLock.rollback();
            }
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return orcamento;
        }
    }
    public boolean settLockOrcamento(Orcamento orcamento) {
        boolean retorno=true;
        int id = orcamento.getId();
        try
        {
            this.sessionLock.update(orcamento);
            this.transacaoLock.commit();
        } catch(Exception e) {
            retorno=false;
            if (this.transacaoLock != null){
                this.transacaoLock.rollback();
            }
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return retorno;
        }
    }
    public boolean unLockOrcamento() {
        boolean retorno=true;
        try
        {
            this.transacaoLock.rollback();
        } catch(Exception e) {
            retorno=false;
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return retorno;
        }
    }
    public List<Orcamento> list(String par) {
        List lista=null;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            String sql = "from Orcamento o";
            if (!par.equals("")) sql=sql + "  where  "+par;
            lista = session.createQuery(sql).list();
        } catch(Exception e)
        {
            lista=null;
            if (session != null){
                session.close();
            }
        } finally
        {
            return lista;
        }
    }
    public boolean remove(Orcamento orcamento) {
        boolean retorno = true;
        try
        {
            sessionLock.delete(orcamento);
            transacaoLock.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (transacaoLock != null){
                transacaoLock.rollback();
            }
        } finally
        {
            if (sessionLock != null){
                sessionLock.close();
            }
            return retorno;
        }
    }
}
